package cn.mazexal.examapi.controller;

import cn.mazexal.examapi.common.models.*;
import cn.mazexal.examapi.models.db.ExamQuestionDO;
import cn.mazexal.examapi.models.enums.ExamTypeEnum;
import cn.mazexal.examapi.models.form.AddUserNoteForm;
import cn.mazexal.examapi.models.form.ReviewUserWrongQuestionForm;
import cn.mazexal.examapi.models.form.UserCollectForm;
import cn.mazexal.examapi.models.qry.ExamLogQry;
import cn.mazexal.examapi.models.qry.IDQry;
import cn.mazexal.examapi.models.qry.UserCollectQry;
import cn.mazexal.examapi.models.vo.*;
import cn.mazexal.examapi.services.CommonService;
import cn.mazexal.examapi.services.ExamService;
import cn.mazexal.examapi.services.QuestionService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Mazexal
 * Date: 2025/5/24
 */
@RestController
@RequestMapping("/userExam")
@Tag(name = "userExam", description = "用户考试接口")
public class UserExamController extends BaseController {

    @Resource
    private QuestionService questionService;

    @Resource
    private CommonService commonService;

    @Resource
    private ExamService examService;

    @PostMapping("/manageUserCollect")
    @Tag(name = "manageUserCollect", description = "管理收藏")
    public SingleResponse<Boolean> manageUserCollect(@RequestBody UserCollectForm form) {
        form.setUserId(getUserId());
        boolean res = questionService.manageUserCollect(form);
        return SingleResponse.of(res);
    }

//    @PostMapping("/removeUserCollect")
//    @Tag(name = "removeUserCollect", description = "删除收藏")
//    public SingleResponse removeUserCollect(@RequestBody IDQry qry) {
//        questionService.removeUserCollect(qry);
//        return SingleResponse.success();
//    }

    @PostMapping("/getUserCollectList")
    @Tag(name = "getUserCollectList", description = "获取收藏列表")
    public SinglePagedResponse<PagedQuestionWithMaterialsVO<UserExamCollectQuestionVO>> getUserCollectList(@RequestBody UserCollectQry qry) {
        qry.setUserId(getUserId());
        PagedQuestionData<UserExamCollectQuestionVO> pagedData = questionService.getUserCollectList(qry);
        PagedQuestionWithMaterialsVO<UserExamCollectQuestionVO> res = new PagedQuestionWithMaterialsVO<>();
        res.setQuestionList(pagedData.getData());
        res.setMaterialsList(pagedData.getMaterialsList());
        return SinglePagedResponse.of(res, pagedData.getTotal(), pagedData.getPageSize(), pagedData.getCurrent());
    }

    @PostMapping("/getUserWrongQuestion")
    @Tag(name = "getUserWrongQuestion", description = "获取错题列表")
    public SinglePagedResponse<PagedQuestionWithMaterialsVO<UserExamWrongQuestionVO>> getUserWrongQuestion(@RequestBody UserCollectQry qry) {
        qry.setUserId(getUserId());
        PagedQuestionData<UserExamWrongQuestionVO> pagedData = questionService.getUserWrongList(qry);
        PagedQuestionWithMaterialsVO<UserExamWrongQuestionVO> res = new PagedQuestionWithMaterialsVO<>();
        res.setQuestionList(pagedData.getData());
        res.setMaterialsList(pagedData.getMaterialsList());
        return SinglePagedResponse.of(res, pagedData.getTotal(), pagedData.getPageSize(), pagedData.getCurrent());
    }


    // 这里先最简单的实现一下
    @PostMapping("/reviewUserWrongQuestion")
    @Tag(name = "reviewUserWrongQuestion", description = "重做错题")
    public SingleResponse reviewUserWrongQuestion(@RequestBody ReviewUserWrongQuestionForm form) {
        questionService.reviewUserWrongQuestion(form);
        return SingleResponse.success();
    }

    @PostMapping("/getUserExamLogList")
    @Tag(name = "getUserExamLogList", description = "获取考试记录列表")
    public PagedResponse<UserExamLogVO> getUserExamLogList(@RequestBody ExamLogQry qry) {
        Long userId = getUserId();
        qry.setUserId(userId);
        if (qry.getExamType() == null) {
            qry.setExamType(1);
        }
        qry.setExamTypes(new ArrayList<>());
        if (qry.getExamType() == 1) {
            qry.getExamTypes().add(ExamTypeEnum.PRACTISE_EXAM.getCode());
            qry.getExamTypes().add(ExamTypeEnum.RANDOM_EXAM.getCode());
        } else if (qry.getExamType() == 2) {
            qry.getExamTypes().add(ExamTypeEnum.AUTO_EXAM.getCode());
            qry.getExamTypes().add(ExamTypeEnum.ORGANIZATION_EXAM.getCode());
            qry.getExamTypes().add(ExamTypeEnum.REAL_EXAM.getCode());
        }
        PagedData<UserExamLogVO> pagedData = questionService.getUserExamLogList(qry);
        return PagedResponse.of(pagedData);
    }

    @PostMapping("/getUserExamLogDetail")
    @Tag(name = "getUserExamLogDetail", description = "获取考试记录详情")
    public SingleResponse<UserExamQuestionLogWithMaterialsVO> getUserExamLogDetail(@RequestBody IDQry qry) {
        List<UserQuestionVO> userQuestionList = questionService.getUserQuestionByLogId(qry.getId());
        List<Long> materialsIds =
                userQuestionList.stream().map(ExamQuestionVO::getMaterialsId).filter(x -> x > 0).toList();
        List<ExamQuestionMaterialsVO> materialsList =
                questionService.getExamQuestionMaterialsList(materialsIds);
        UserExamQuestionLogWithMaterialsVO res = new UserExamQuestionLogWithMaterialsVO();
        res.setQuestionList(userQuestionList);
        res.setMaterialsList(materialsList);
        res.setLogId(qry.getId());
        examService.wrapUserExamLog(res);
        return SingleResponse.of(res);
    }

    @PostMapping("/addUserNote")
    @Tag(name = "addUserNote", description = "添加笔记")
    public SingleResponse addUserNote(@RequestBody AddUserNoteForm addUserNoteForm) {
        questionService.addOrUpdateUserQuestionNote(getUserId(), addUserNoteForm.getQuestionId(),
                addUserNoteForm.getNote());
        return SingleResponse.success();
    }
}
